Skip to main content

Q-1: Attempt the following (5 Marks)

Questions

a) What is the key difference between Linear Layout and Relative Layout in Android UI design
b) How do you animate a Bitmap in an Android application
c) Describe the process of creating a Table Layout in an Android user interface
d) What are the steps to add a new record in an SQLite database in Android
e) How do you implement a scrolling mechanism in a screen with multiple layouts in Android


Answers

a) Key difference between Linear Layout and Relative Layout

AspectLinearLayoutRelativeLayout
ArrangementArranges views in a single row or columnPositions views relative to each other or parent
OrientationUses android:orientation (horizontal/vertical)No orientation attribute needed
PositioningSequential arrangementRelative positioning using attributes
PerformanceBetter for simple layoutsCan be complex but flexible
NestingMay require nested layouts for complex UICan avoid nesting with relative positioning
WeightSupports layout_weight for proportional sizingNo weight concept

Key Differences:

  1. LinearLayout arranges child views in a single direction (horizontal or vertical)
  2. RelativeLayout allows child views to be positioned relative to each other or the parent
  3. LinearLayout is simpler but may require nesting for complex layouts
  4. RelativeLayout is more flexible but can be more complex to implement

b) How to animate a Bitmap in Android application

Method 1: Using Animation Drawable

<!-- res/drawable/bitmap_animation.xml -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/frame1" android:duration="100" />
<item android:drawable="@drawable/frame2" android:duration="100" />
<item android:drawable="@drawable/frame3" android:duration="100" />
</animation-list>
// In Activity
ImageView imageView = findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.bitmap_animation);
AnimationDrawable animation = (AnimationDrawable) imageView.getBackground();
animation.start();

Method 2: Using ObjectAnimator

// Fade animation
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);
fadeIn.setDuration(1000);
fadeIn.start();

// Scale animation
ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 1.5f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 1.5f);
AnimatorSet scaleSet = new AnimatorSet();
scaleSet.playTogether(scaleX, scaleY);
scaleSet.setDuration(1000);
scaleSet.start();

Method 3: Using Canvas Animation

public class AnimatedBitmapView extends View {
private Bitmap bitmap;
private float x = 0;

public AnimatedBitmapView(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_bitmap);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, x, 100, null);
x += 5; // Move bitmap
if (x > getWidth()) x = 0; // Reset position
invalidate(); // Trigger redraw
}
}

c) Process of creating a Table Layout in Android

Step 1: Define TableLayout in XML

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="1">

<!-- Table Row 1 -->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:text="Name:"
android:padding="10dp"
android:textStyle="bold" />

<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Enter name" />

</TableRow>

<!-- Table Row 2 -->
<TableRow>
<TextView
android:text="Email:"
android:padding="10dp"
android:textStyle="bold" />

<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Enter email"
android:inputType="textEmailAddress" />
</TableRow>

<!-- Table Row 3 -->
<TableRow>
<TextView
android:text="Phone:"
android:padding="10dp"
android:textStyle="bold" />

<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Enter phone"
android:inputType="phone" />
</TableRow>

</TableLayout>

Step 2: Key Attributes

  • android:stretchColumns: Specifies which columns to stretch
  • android:shrinkColumns: Specifies which columns to shrink
  • android:collapseColumns: Specifies which columns to collapse

Step 3: Programmatic Creation

TableLayout tableLayout = new TableLayout(this);
TableRow tableRow = new TableRow(this);

TextView textView = new TextView(this);
textView.setText("Label:");
textView.setPadding(10, 10, 10, 10);

EditText editText = new EditText(this);
editText.setHint("Enter value");

tableRow.addView(textView);
tableRow.addView(editText);
tableLayout.addView(tableRow);

d) Steps to add a new record in SQLite database in Android

Step 1: Create Database Helper Class

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyDatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_EMAIL + " TEXT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

Step 2: Add Record Method

public boolean addUser(String name, String email) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put(COLUMN_NAME, name);
contentValues.put(COLUMN_EMAIL, email);

long result = db.insert(TABLE_NAME, null, contentValues);
db.close();

return result != -1; // Returns true if insert successful
}

Step 3: Using the Method in Activity

public class MainActivity extends AppCompatActivity {
private DatabaseHelper databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

databaseHelper = new DatabaseHelper(this);

// Add a new record
boolean isInserted = databaseHelper.addUser("John Doe", "john@email.com");

if (isInserted) {
Toast.makeText(this, "Record added successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Failed to add record", Toast.LENGTH_SHORT).show();
}
}
}

Complete Steps:

  1. Create SQLiteOpenHelper subclass
  2. Define database schema in onCreate()
  3. Create ContentValues object
  4. Put key-value pairs into ContentValues
  5. Use insert() method to add record
  6. Close database connection
  7. Handle success/failure response

e) Implementing scrolling mechanism with multiple layouts

Method 1: Using ScrollView

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- First Layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#E3F2FD">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout"
android:layout_centerInParent="true" />

</RelativeLayout>

<!-- Second Layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#F3E5F5"
android:orientation="horizontal">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2" />

</LinearLayout>

<!-- Third Layout -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#E8F5E8">

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher_background" />

</FrameLayout>

</LinearLayout>

</ScrollView>

Method 2: Using NestedScrollView (for complex scrolling)

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- Multiple layouts here -->

</LinearLayout>

</androidx.core.widget.NestedScrollView>

Method 3: Programmatic Scrolling

ScrollView scrollView = findViewById(R.id.scrollView);

// Scroll to specific position
scrollView.smoothScrollTo(0, 500);

// Scroll to top
scrollView.fullScroll(ScrollView.FOCUS_UP);

// Scroll to bottom
scrollView.fullScroll(ScrollView.FOCUS_DOWN);

// Custom scroll listener
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollY = scrollView.getScrollY();
// Handle scroll position changes
}
});

Key Points:

  1. ScrollView can contain only one direct child
  2. Use LinearLayout or RelativeLayout as container for multiple layouts
  3. Set android:fillViewport="true" for proper scrolling behavior
  4. NestedScrollView is better for complex scrolling scenarios
  5. Avoid putting ListView or RecyclerView inside ScrollView

← Back to Question Paper